import 'package:flutter/material.dart';

import '../util.dart';

class PopupAlertView extends StatelessWidget {
  const PopupAlertView({
    super.key,
    this.title,
    this.msg,
    this.content,
    this.cancelColor,
    this.cancelText = '取消',
    this.sureColor,
    this.sureText = '确认',
    this.conditionDismiss,
    this.hasHeaderLine = true,
    this.hasFooterLine = false,
    this.margin,
    this.backgroundColor,
  });
  final String? title;
  final String? msg;
  final Widget? content;
  final String? cancelText;
  final String? sureText;
  final Color? cancelColor;
  final Color? sureColor;
  final BoolResultCallback? conditionDismiss;
  final bool hasHeaderLine;
  final bool hasFooterLine;
  final Color? backgroundColor;

  final EdgeInsets? margin;
  @override
  Widget build(BuildContext context) {
    void sureEvent() async {
      Get.back(result: true);
    }

    void dismiss() async {
      Get.back(result: false);
    }

    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        Card(
          clipBehavior: Clip.hardEdge,
          margin: margin ?? EdgeInsets.symmetric(horizontal: 25.r),
          child: Column(
            children: [
              if (title != null)
                Container(
                  padding: EdgeInsets.symmetric(
                      vertical: (msg == null && content == null) ? 30 : 15),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      border: hasHeaderLine && (msg != null || content != null)
                          ? const Border(
                              bottom: BorderSide(
                                  color: Color(0xffdcdfe6), width: 0.5))
                          : null),
                  child: Text(
                    title!,
                    style: TextStyle(
                        color: const Color(0xff333333),
                        fontSize: 18.sp,
                        fontWeight: FontWeight.bold),
                  ),
                ),
              if (msg != null)
                Container(
                  padding:
                      const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                  child: Text(
                    msg!,
                    textAlign: TextAlign.left,
                    style: TextStyle(
                      color: const Color(0xff666666),
                      fontSize: 15.sp,
                    ),
                  ),
                ),
              if (content != null) content!,
              if (sureText != null || cancelText != null)
                BtnBar(
                  onTap: (idx) {
                    if (idx == 0) {
                      //取消
                      dismiss();
                    } else {
                      //确定
                      if (conditionDismiss != null) {
                        if (conditionDismiss!()) {
                          sureEvent();
                        }
                      } else {
                        sureEvent();
                      }
                    }
                  },
                  left: cancelText,
                  right: sureText,
                  hasTopLine: hasFooterLine,
                ),
            ],
          ),
        ),
      ],
    );
  }
}

class PopupAlertGradientView extends StatelessWidget {
  const PopupAlertGradientView({
    super.key,
    this.title,
    this.msg,
    this.content,
    this.cancelText = '取消',
    this.sureText = '确认',
    this.conditionDismiss,
    this.margin,
    List<Color>? gradientColors,
  }) : gradientColors = const [Color(0xffC0BCFF), Color(0xffffffff)];
  final String? title;
  final String? msg;
  final Widget? content;
  final String? cancelText;
  final String? sureText;

  final BoolResultCallback? conditionDismiss;

  final List<Color> gradientColors;

  final EdgeInsets? margin;

  void onSure() {
    Get.back(result: true);
  }

  @override
  Widget build(BuildContext context) {
    void dismiss() async {
      Get.back(result: false);
    }

    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: [
        Card(
          clipBehavior: Clip.hardEdge,
          margin: margin ?? EdgeInsets.symmetric(horizontal: 25.r),
          color: Colors.white,
          child: Column(
            children: [
              if (title != null)
                Container(
                  padding: EdgeInsets.symmetric(
                      vertical: (msg == null && content == null) ? 30 : 15),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      gradient: LinearGradient(
                          colors: gradientColors,
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter)),
                  child: Text(
                    title!,
                    style: TextStyle(
                        color: const Color(0xff333333),
                        fontSize: 18.sp,
                        fontWeight: FontWeight.bold),
                  ),
                ),
              if (msg != null)
                Container(
                  padding:
                      const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                  child: Text(
                    msg!,
                    textAlign: TextAlign.left,
                    style: TextStyle(
                      color: const Color(0xff666666),
                      fontSize: 15.sp,
                    ),
                  ),
                ),
              if (content != null) content!,
              if (sureText != null || cancelText != null)
                Padding(
                  padding: EdgeInsets.fromLTRB(20.r, 10.r, 20.r, 10.r),
                  child: SeparatorRow(
                      children: [
                        if (cancelText?.isNotEmpty == true)
                          Expanded(
                            child: OutlinedButton(
                                onPressed: dismiss,
                                style: const ButtonStyle(
                                    side: WidgetStatePropertyAll(
                                        BorderSide(color: Color(0xff7774FC)))),
                                child: Text(
                                  cancelText!,
                                  style: TextStyle(
                                      color: const Color(0xff7774FC),
                                      fontSize: 16.sp),
                                )),
                          ),
                        if (sureText?.isNotEmpty == true)
                          Expanded(
                            child: FilledButton(
                              onPressed: () {
                                //确定
                                if (conditionDismiss != null) {
                                  if (conditionDismiss!()) {
                                    onSure();
                                  }
                                } else {
                                  onSure();
                                }
                              },
                              child: Text(
                                sureText!,
                                style: TextStyle(
                                    color: Colors.white, fontSize: 16.sp),
                              ),
                            ),
                          )
                      ],
                      separatorBuilder: (_) => SizedBox(
                            width: 24.r,
                          )),
                ),
            ],
          ),
        ),
      ],
    );
  }
}

class _PopupAlertGradientInputView extends PopupAlertGradientView {
  const _PopupAlertGradientInputView({
    super.key,
    required this.controller,
    this.placeholder = '请输入',
    this.maxLines = 5,
    super.title,
    super.msg,
    super.cancelText = '取消',
    super.sureText = '提交',
    super.conditionDismiss,
    super.margin,
    List<Color>? gradientColors,
  });

  final TextEditingController controller;
  final String placeholder;
  final int maxLines;

  @override
  void onSure() {
    if (controller.text.isEmpty) {
      Hud.toast(placeholder);
      return;
    }
    Get.back(result: controller.text);
  }

  @override
  Widget? get content => Padding(
        padding: EdgeInsets.symmetric(horizontal: 18.r),
        child: TextField(
          controller: controller,
          style: TextStyle(color: const Color(0xff333333), fontSize: 14.sp),
          maxLines: maxLines,
          decoration: InputDecoration(
            hintText: placeholder,
            hintStyle:
                TextStyle(color: const Color(0xff999999), fontSize: 14.sp),
            filled: true,
            fillColor: const Color(0xffEFEFEF),
            border: const OutlineInputBorder(borderSide: BorderSide.none),
            enabledBorder:
                const OutlineInputBorder(borderSide: BorderSide.none),
            focusedBorder:
                const OutlineInputBorder(borderSide: BorderSide.none),
          ),
        ),
      );
}

class PopupAlertGradientInputView extends StatefulWidget {
  const PopupAlertGradientInputView({
    super.key,
    this.title,
    this.msg,
    this.cancelText = '取消',
    this.sureText = '提交',
    this.conditionDismiss,
    this.margin,
    this.placeholder = '请输入',
    this.maxLines = 3,
    List<Color>? gradientColors,
  }) : gradientColors = const [Color(0xffC0BCFF), Color(0xffffffff)];
  final String? title;
  final String? msg;
  final String? cancelText;
  final String? sureText;

  final BoolResultCallback? conditionDismiss;

  final List<Color> gradientColors;

  final EdgeInsets? margin;
  final String placeholder;
  final int maxLines;
  @override
  State<PopupAlertGradientInputView> createState() =>
      _PopupAlertGradientInputViewState();
}

class _PopupAlertGradientInputViewState
    extends State<PopupAlertGradientInputView> {
  final editCtrl = TextEditingController();

  @override
  void initState() {
    editCtrl.text = widget.msg ?? '';
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return _PopupAlertGradientInputView(
      controller: editCtrl,
      title: widget.title,
      msg: widget.msg,
      placeholder: widget.placeholder,
      cancelText: widget.cancelText,
      sureText: widget.sureText,
      conditionDismiss: widget.conditionDismiss,
      margin: widget.margin,
      maxLines: widget.maxLines,
      gradientColors: widget.gradientColors,
    );
  }
}
